﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using blackhouse.mojoPortalExtend.Urls;
using blackhouse.NemoBlog.Business;

namespace blackhouse.NemoBlog.Web {
    public class TagUrlSupport : UrlSupport {

        #region Fields

        private NemoBlogTag tag;
        private int urlVariantIdx = 0;

        #endregion

        #region Constructors

        public TagUrlSupport(NemoBlogTag tag) {
            this.tag = tag;

            this.OldUrl = this.tag.TagUrl;
            this.NewUrl = this.GetNewUrl(this.urlVariantIdx);
            this.RealUrl = String.Format("~/Default.aspx?tag={0}", this.tag.TagId);
            this.Guid = this.tag.TagGuid;
        }

        #endregion

        #region Methods

        public override void Save() {
            this.tag.TagUrl = this.NewUrl;
            this.tag.Save();
        }

        public override void ChangeToOtherNewUrl() {
            if (this.NewUrl == GetNewUrl(this.urlVariantIdx))
                this.urlVariantIdx++;
            this.NewUrl = GetNewUrl(this.urlVariantIdx);
        }

        private string GetNewUrl(int variantIdx) {
            switch (variantIdx) {
                case 0:
                    return "~/" + this.tag.Title.RemovePolishChars().CleanStringForUrl();
                case 1:
                    return "~/posts-" + this.tag.Title.RemovePolishChars().CleanStringForUrl();
                case 2:
                    return "~/blackhouse-" + this.tag.Title.RemovePolishChars().CleanStringForUrl();
                default:
                    throw new UrlInUseException(this);
            }
        }

        public void SetUserNewUrl(string url) {
            this.NewUrl = url;
        }

        #endregion

    }
}